package com.itcm.skymail.search.controller;

import com.itcm.common.exception.BizCodeEnum;
import com.itcm.common.to.es.SkuEsModel;
import com.itcm.common.utils.R;
import com.itcm.skymail.search.service.ProductSaveService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * 描述:
 *
 * @author cm_fighting
 * @create 2020-10-13 4:08 下午
 */
@RestController
@RequestMapping("/search/save")
@Slf4j
public class ElasticsearchSaveController {
    @Resource
    private ProductSaveService productSaveService;

    // 上架商品
    @PostMapping("/product")
    public R productUp(@RequestBody List<SkuEsModel> skuEsModels) {
        boolean b = false;
        try {
            b = productSaveService.up(skuEsModels);
        } catch (Exception e) {
            log.error("商品上架错误: {}", e.getMessage());
            return R.error(BizCodeEnum.PRODUCT_UP_EXCEPTION.getCode(), BizCodeEnum.PRODUCT_UP_EXCEPTION.getMsg());
        }
        if (b) {
            return R.ok();
        } else {
            return R.error(BizCodeEnum.PRODUCT_UP_EXCEPTION.getCode(), BizCodeEnum.PRODUCT_UP_EXCEPTION.getMsg());
        }
    }
}
